package com.cws.frame;

import java.util.Properties;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.cws.frame.interceptor.AuthInterceptor;
import com.cws.frame.interceptor.CWSExceptionHandler;
import com.cws.frame.plugin.QuartzPlugin;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;

/**
 * API引导式配置
 */
public class AppConfig extends JFinalConfig {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		
		Properties config = loadPropertyFile("conf.properties");
		AppInit.initConfig(config);
		
		me.setDevMode(getPropertyToBoolean("devMode", false));
		me.setError404View("/error/404.html");
		me.setError500View("/error/500.html");
		me.setViewType(ViewType.JSP);
	}

	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		AppInit.initRoutes(me);
	}

	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		
		// 配置Druid数据库连接池插件
		DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password")
				.trim());
		
		
		druidPlugin.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType(getProperty("dbType"));
		druidPlugin.addFilter(wall);
		
		me.add(druidPlugin);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		me.add(arp);
		AppInit.initArpMapping(arp);
		
		//配置任务调度插件
		me.add(new QuartzPlugin(AppInit.getSchedulerFactory(),null));
	}

	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		//统一异常拦截器
		me.add(new CWSExceptionHandler());
		//鉴权拦截器
		me.add(new AuthInterceptor());
	}

	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {

	}

	/**
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}
}
